package de.bml.versandanzeige;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class Gegenstandstyp {

	public static final String TABLE_NAME = "gegenstandstypen";
	public static final String COLUMN_LABEL = "label";

	// Datebase fields
	private static SQLiteOpenHelper dbHelper;
	private static SQLiteDatabase db;

	// Database creation sql statement
	private static final String TABLE_CREATE = "create table " + TABLE_NAME
			+ "( " + SQLiteOpenHelper.COLUMN_ID	+ " integer primary key autoincrement, " + COLUMN_LABEL	+ " text not null);";

	private String id;

	public Gegenstandstyp(String label) {
		label.trim();
		for(Gegenstandstyp typ : getAll()) {
			if(typ.getLabel().equals(label)) {
				id = Long.toString(typ.getId());
				return;
			}
		}
		ContentValues values = new ContentValues();
		values.put(COLUMN_LABEL, label);
		id = Long.toString(db.insert(TABLE_NAME, null, values));
	}

	public Gegenstandstyp(long id) {
		this.id = Long.toString(id);
	}
	
	public long getId() {
		return Long.valueOf(id);
	}

	public static Gegenstandstyp get(long id) {
		return new Gegenstandstyp(id);
	}

	public static Gegenstandstyp create(String label) {
		return new Gegenstandstyp(label);
	}

	public String getLabel() {
		Cursor c = db.query(TABLE_NAME, new String[] { COLUMN_LABEL },
				SQLiteOpenHelper.COLUMN_ID + "=?", new String[] { id }, null,
				null, null);
		c.moveToFirst();
		String result = c.getString(0);
		c.close();
		return result;
	}

	public Gegenstandstyp setLabel(String label) {
		label.trim();
		ContentValues values = new ContentValues();
		values.put(COLUMN_LABEL, label);
		db.update(TABLE_NAME, values, SQLiteOpenHelper.COLUMN_ID + "=?",
				new String[] { id });
		return this;
	}

	// Will be used be the ArrayAdapter of a ListView
	@Override
	public String toString() {
		return getLabel();
	}

	public static void onCreate(SQLiteDatabase db) {
		db.execSQL(TABLE_CREATE);
	}

	public static void onUpgrade(SQLiteDatabase db) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
		onCreate(db);
	}

	public void delete() {
		System.out.println("Gegenstandstyp deleted with id: " + id);
		db.delete(TABLE_NAME, SQLiteOpenHelper.COLUMN_ID + " = " + id, null);
		id = null;
	}

	public static void deleteAll() {
		System.out.println("All Gegenstandytyp deleted.");
		db.delete(TABLE_NAME, null, null);
	}
	
	public static List<Gegenstandstyp> getAll() {
		List<Gegenstandstyp> gegenstandstypen = new ArrayList<Gegenstandstyp>();

		Cursor cursor = db.query(TABLE_NAME,
				new String[] { SQLiteOpenHelper.COLUMN_ID }, null, null, null,
				null, COLUMN_LABEL);
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			gegenstandstypen.add(Gegenstandstyp.get(cursor.getLong(0)));
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return gegenstandstypen;
	}

	public static void reconnectDB() {
		db = dbHelper.getWritableDatabase();
	}

	public static void disconnectDB() {
		db.close();
	}

	public static void init(Context context) {
		dbHelper = new SQLiteOpenHelper(context);
		db = dbHelper.getWritableDatabase();
	}

}
